package com.cqut.forum.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cqut.forum.entity.Picture;
import com.cqut.forum.entity.Review;
import com.cqut.forum.entity.User;
import com.cqut.forum.service.PictureReplyService;
import com.cqut.forum.service.PictureService;

/**
 * 图片评论处理action
 * @author yangxun
 */
@Controller
public class PictureReplyAction {
	
	@Resource
	private PictureService pictureService;
	@Resource
	private PictureReplyService pictureReplyService;
	
	/**
	 * 图片评论处理方法
	 * @param request
	 * @param pictureId 图片Id
	 * @param contents 评论内容
	 * @return 返回一个map对象，并采用spring的json机制把数据传送到客户端
	 */
	@SuppressWarnings("deprecation")
	@RequestMapping("/picture/picture_reply")
	@ResponseBody
	public Map<String, Object> pictureReply(HttpServletRequest request,int pictureId,String content){
		//获取人员信息
		User user = (User)request.getSession().getAttribute("user");
		//封装数据返回前台展示
		Map<String, Object> result = new HashMap<String, Object>();
		//获取被评论图片对象
		Picture picture = pictureService.getById(pictureId);
		if(picture.getOwnerId()==user.getUserId()){
			return result;
		}
		//更新图片最后评论时间，这是一个辅助属性。该属性用于图片信息展示时的先后顺序排序
		picture.getLastReviewTime();
		picture.setLastReviewTime(new Date());
		
		picture = pictureService.update(picture);
		
		//创建评论实体对象
		Review review = new Review();
		review.setIsPublic(picture.getIsPublic());
		review.setPicture(picture);
		review.setReviewContents(content);
		review.setReviewState(true);
		review.setReviewTime(new Date());
		review.setReviewUserId(user.getUserId());
		
		//保存评论并返回保存的实体对象
		review = pictureReplyService.save(review);
		
		result.put("image", user.getUserPhoto());
		result.put("name", user.getUserName());
		result.put("content", review.getReviewContents());
		result.put("time", ((Date)review.getReviewTime()).toLocaleString());
		result.put("userId", user.getUserId());
		result.put("pictureId", picture.getPictureId());
		/*
		 * Map<String, Object>	map = new HashMap<String, Object>();
			map.put("userId", m.get("user_id"));
			map.put("userName", m.get("user_name"));
			map.put("userPhoto", m.get("user_photo"));
			map.put("replyContent", m.get("review_contents"));
			map.put("replyTime", ((Date)m.get("review_time")).toLocaleString());
			map.put("pictureId", m.get("picture_id"));
		 *  */
		return result;
	}
	
	@RequestMapping("/picture/get_picture_reply")
	@ResponseBody
	public Map<String, Object> getPictureReply(int pictureId){
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> owner = new HashMap<String, Object>();
		List<Map<String, Object>> pictureReplys = pictureReplyService.getPictureReplys(pictureId);
		result.put("replys", pictureReplys);
		
		User user = pictureService.getPictureOwner(pictureId);
		if(user!=null){
			owner.put("userPhoto", user.getUserPhoto());
			owner.put("userName", user.getUserName());
			owner.put("userSign", user.getUserSign());
		}
		result.put("owner", owner);
		return result;
	}
}
